package marksys.free.com.marksys.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.databinding.ActivityLoginBinding;
import marksys.free.com.marksys.nettool.Constant;
import marksys.free.com.marksys.nettool.MyApi;
import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.vo.User;
import marksys.free.com.marksys.ui.main.MainActivity;
import marksys.free.com.marksys.utils.SharedPreferencesUtils;
import marksys.free.com.marksys.utils.ShowErrTool;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        //免登录
        String user = SharedPreferencesUtils.getString(Constant.UserToken, "");
        if (!"".equals(user)) {
            String realName = SharedPreferencesUtils.getString(Constant.realName, "");
            int type = SharedPreferencesUtils.getInt(Constant.type, 0);
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra(MainActivity.NAME, realName);
            intent.putExtra(MainActivity.TYPE, type);
            startActivity(intent);
            this.finish();
        }
        binding.num.setOnFocusChangeListener((v, hasFocus) -> {
            if (v instanceof EditText) {
                ((EditText) v).setCursorVisible(hasFocus);
            }
        });
        binding.password.setOnFocusChangeListener((v, hasFocus) -> {
            if (v instanceof EditText) {
                ((EditText) v).setCursorVisible(hasFocus);
            }
        });
        binding.loginBtn.setOnClickListener(v -> {
            String num = binding.num.getText().toString();
            String pwd = binding.password.getText().toString();
            if (TextUtils.isEmpty(num)) {
                Toast.makeText(LoginActivity.this, "帐号不能为空", Toast.LENGTH_SHORT).show();
                binding.num.requestFocus();
            } else if (TextUtils.isEmpty(pwd)) {
                Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                binding.password.requestFocus();
            } else {
                login(num, pwd);
            }
        });

    }

    private static final String TAG = "LoginActivity";

    void login(String loginName, String password) {
        MyApi.login(loginName, password).observe(this, (Resource<User> apiResultResource) -> {
            Log.i(TAG, "login: " + apiResultResource);
            assert null != apiResultResource;
            switch (apiResultResource.status) {
                case LOADING:
                    //showRequestPictureDialog progress bar加载框
                    //Snackbar.make(view, "Loading", Snackbar.LENGTH_SHORT).showRequestPictureDialog();
                    break;
                case SUCCESS:
                    // save userToken save imtoken
                    assert apiResultResource.data != null;
                    User user = apiResultResource.data;
                    if (apiResultResource.errorCode == 0) {
                        SharedPreferencesUtils.saveString(Constant.UserToken, user.token);
                        SharedPreferencesUtils.saveString(Constant.realName, user.realName);
                        SharedPreferencesUtils.saveInt(Constant.type, user.type);
                        String realName = SharedPreferencesUtils.getString(Constant.realName, "");
                        int type = SharedPreferencesUtils.getInt(Constant.type, 0);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra(MainActivity.NAME, realName);
                        intent.putExtra(MainActivity.TYPE, type);
                        startActivity(intent);
                        this.finish();
                    }
                    break;
                case ERROR:
                    //errorProcess.get().process(apiResultResource);
                    ShowErrTool.show(apiResultResource.errorCode, LoginActivity.this, apiResultResource.errorMessage);
                    break;
                default:
                    break;
            }
        });
    }

}
